package org.zhb.sql.query.core.util;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 日期工具类。
 * 
 * @author dht
 * @version 1.0
 */
public class DateUtil {
	public static final String DEFAULT_DATE_FORMAT_PATTERN = "yyyy-MM-dd";
	public static final String DEFAULT_DATE_TIME_FORMAT_PATTERN = "yyyy-MM-dd hh:mm:ss";
	private static final DateFormat df = new SimpleDateFormat(DEFAULT_DATE_FORMAT_PATTERN);
	private static final DateFormat dtf = new SimpleDateFormat(DEFAULT_DATE_TIME_FORMAT_PATTERN);

	public static Date toDate(String dateStr) {
		try {
			return df.parse(dateStr);
		} catch (ParseException ex) {
			ex.printStackTrace();
			return null;
		}
	}

	public static Date toDateTime(String dateStr) {
		try {
			return dtf.parse(dateStr);
		} catch (ParseException ex) {
			ex.printStackTrace();
			return null;
		}
	}

	public static Timestamp toTimestamp(String dateTimeStr) {
		try {
			return new Timestamp(dtf.parse(dateTimeStr).getTime());
		} catch (ParseException ex) {
			ex.printStackTrace();
			return null;
		}
	}

	public static String formatDate(Date date) {
		try {
			return df.format(date);
		} catch (Exception ex) {
			ex.printStackTrace();
			return null;
		}
	}

	public static String formatDateTime(Date date) {
		try {
			return dtf.format(date);
		} catch (Exception ex) {
			ex.printStackTrace();
			return null;
		}
	}

}
